package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.Agent;
import tech.aistar.entity.CotenancyRequire;
import tech.aistar.entity.House;
import tech.aistar.entity.Tenant;
import tech.aistar.service.ICotenancyRequireService;
import tech.aistar.service.IHouseService;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/post")
public class PostController {

    public static Date stringDateUtil(String dateStr) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.parse(dateStr);
    }

    @Autowired
    private IHouseService houseService;
    @Autowired
    private ICotenancyRequireService cotenancyRequireService;
    @RequestMapping("/middleTogether")
    public String postHomeTogether(HttpServletRequest request){
        Integer id = null;

        if(request.getParameter("agent_id")!=""){
            id = Integer.valueOf(request.getParameter("agent_id"));
        }
        request.setAttribute("agent_id",id);

        return "/home_page/postHomeTogether";
    }

    @RequestMapping("/middleWhole")
    public String postHomeWhole(HttpServletRequest request){
        Integer id = null;

        if(request.getParameter("agent_id")!=""){
            id = Integer.valueOf(request.getParameter("agent_id"));
        }
        request.setAttribute("agent_id",id);

        return "/home_page/postHomeWhole";
    }


    @RequestMapping("/addHouse")
    public String addHouse(HttpServletRequest request) throws ParseException {

        String agentId = request.getParameter("agentId");
        String landlordId = request.getParameter("landlordId");
        String evaluate = request.getParameter("evaluate");
        String require = request.getParameter("require");
        String picture = request.getParameter("picture");
        String type = request.getParameter("type");
        String area = request.getParameter("area");
        String floor = request.getParameter("floor");
        String car = request.getParameter("car");
        String date = request.getParameter("date");
        String direction = request.getParameter("direction");
        String city = request.getParameter("city");
        String rent = request.getParameter("rent");
        String state = request.getParameter("state");
        String money = request.getParameter("money");

        House house = new House();
        house.setAgentId(Integer.valueOf(agentId));
        house.setLandlordId(Integer.valueOf(landlordId));
        house.setHouseEvaluate(evaluate);
        house.setLandlordRequire(require);
        house.setPropertyOwnershipCertificate(picture);
        house.setHouseType(type);
        house.setArea(area);
        house.setFloor(Integer.valueOf(floor));
        house.setParkPlace(car);
        house.setTenancy(stringDateUtil(date));
        house.setOrientate(direction);
        house.setRegion(city);
        house.setRentType(rent);
        house.setHouseState(state);
        house.setRentMoney(Double.valueOf(money));


        houseService.insertHouse(house);


        return "/home_page/home";
    }




    @RequestMapping("/addHouse2")
    public String addHouse2(HttpServletRequest request) throws ParseException {

        String agentId = request.getParameter("agentId");
        String landlordId = request.getParameter("landlordId");
        String evaluate = request.getParameter("evaluate");
        String require = request.getParameter("require");
        String picture = request.getParameter("picture");
        String type = request.getParameter("type");
        String area = request.getParameter("area");
        String floor = request.getParameter("floor");
        String car = request.getParameter("car");
        String date = request.getParameter("date");
        String direction = request.getParameter("direction");
        String city = request.getParameter("city");
        String rent = request.getParameter("rent");
        String state = request.getParameter("state");
        String money = request.getParameter("money");


        House house = new House();
        house.setAgentId(Integer.valueOf(agentId));
        house.setLandlordId(Integer.valueOf(landlordId));
        house.setHouseEvaluate(evaluate);
        house.setLandlordRequire(require);
        house.setPropertyOwnershipCertificate(picture);
        house.setHouseType(type);
        house.setArea(area);
        house.setFloor(Integer.valueOf(floor));
        house.setParkPlace(car);
        house.setTenancy(stringDateUtil(date));
        house.setOrientate(direction);
        house.setRegion(city);
        house.setRentType(rent);
        house.setHouseState(state);
        house.setRentMoney(Double.valueOf(money));

        houseService.insertHouse(house);

        Integer house_id = houseService.selectHouseId(Integer.valueOf(agentId),Integer.valueOf(landlordId),evaluate,require,picture,type,area,Integer.valueOf(floor),car,stringDateUtil(date),direction,city,rent,state,Double.valueOf(money));

        String family = request.getParameter("box1");
        String forbidPet = request.getParameter("box2");
        String tenantStable = request.getParameter("box3");
        String workRestNormal = request.getParameter("box4");
        String forbidSmoke = request.getParameter("box5");
        String detailRequire = request.getParameter("box6");
        CotenancyRequire cotenancyRequire = new CotenancyRequire(Integer.valueOf(landlordId),family,forbidPet,tenantStable,workRestNormal,forbidSmoke,house_id,detailRequire);

        cotenancyRequireService.insert(cotenancyRequire);


        return "/home_page/home";
    }

}
